package com.github.catvod.spider.merge;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* loaded from: classes.dex */
public class jP {
    private static final char[] h = "0123456789ABCDEF".toCharArray();

    public static String BQ(String str, String str2, String str3) {
        try {
            byte[] h2 = aF.h(str);
            SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes(), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(str3.getBytes());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, secretKeySpec, ivParameterSpec);
            return new String(cipher.doFinal(h2), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String I(String str, String str2, String str3, String str4) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(str3.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
            cipher.init(2, secretKeySpec, new IvParameterSpec(str4.getBytes()));
            return new String(cipher.doFinal(aF.h(str)), str2);
        } catch (Exception unused) {
            return null;
        }
    }

    public static String Qy(String str, String str2, String str3) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(oq(str2.getBytes()).getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(1, secretKeySpec, new IvParameterSpec(u(str3)));
        return aF.V0(cipher.doFinal(str.getBytes("UTF-8"))).toString();
    }

    public static String V0(String str, String str2) {
        if (str2 != null) {
            try {
                if (str2.length() == 32) {
                    SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes("UTF-8"), "AES");
                    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                    cipher.init(2, secretKeySpec);
                    return new String(cipher.doFinal(aF.h(str)), "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        throw new IllegalArgumentException("密钥长度必须为 32 字节");
    }

    public static String W(String str, String str2, String str3) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(oq(str2.getBytes()).getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(1, secretKeySpec, new IvParameterSpec(str3.getBytes()));
        return aF.V0(cipher.doFinal(str.getBytes("UTF-8"))).toString();
    }

    public static String h(String str, String str2) {
        try {
            if (str2 == null) {
                System.out.print("Key为空null");
                return null;
            }
            if (str2.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes("utf-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(2, secretKeySpec);
            try {
                return new String(cipher.doFinal(aF.h(str)), "utf-8");
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception e2) {
            System.out.println(e2.toString());
            return null;
        }
    }

    public static String j(String str, String str2) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes("utf-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, secretKeySpec);
        return aF.V0(cipher.doFinal(str.getBytes("utf-8")));
    }

    public static String oC(String str, String str2, String str3) {
        try {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(str3.getBytes("UTF-8"));
            SecretKeySpec secretKeySpec = new SecretKeySpec(str2.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(2, secretKeySpec, ivParameterSpec);
            return new String(cipher.doFinal(aF.h(str)), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static Key oq(byte[] bArr) {
        return new SecretKeySpec(bArr, "AES");
    }

    public static String q(String str, String str2, String str3) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(oq(str2.getBytes()).getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(2, secretKeySpec, new IvParameterSpec(str3.getBytes()));
        return new String(cipher.doFinal(aF.h(str)), "UTF-8");
    }

    public static byte[] u(String str) {
        if (str.length() < 1) {
            return null;
        }
        byte[] bArr = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            int i2 = i * 2;
            int i3 = i2 + 1;
            bArr[i] = (byte) ((Integer.parseInt(str.substring(i2, i3), 16) * 16) + Integer.parseInt(str.substring(i3, i2 + 2), 16));
        }
        return bArr;
    }
}
